define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/saleer/index' + location.search,
                    add_url: 'xiservice/saleer/add',
                    edit_url: 'xiservice/saleer/edit',
                    del_url: 'xiservice/saleer/del',
                    multi_url: 'xiservice/saleer/multi',
                    import_url: 'xiservice/saleer/import',
                    table: 'xiservice_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,     
                showExport: false,
                showToggle: false,
                showColumns: false,                
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), visible:false,operate: 'LIKE'},
                        {field: 'mobile_text', title: __('Mobile'), operate: false},
                        {field: 'under_num', title: __('下级人数'), operate: false},
                        {field: 'can_money', title: __('Can_money'), operate: false},
                        {field: 'freeze_money', title: __('Freeze_money'), operate: false},
                        {field: 'total_money', title: __('Total_money'), operate: false},
                        {
                            field: 'buttons',
                            width: "120px",
                            title: __('收入明细'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('收入明细'),
                                    title: __('收入明细'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-list',
                                    url: 'xiservice/benfit/index?ids={id}'
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                            operate:false
                        },                           
                        {field: 'status', title: __('Status'), searchList: {"1":__('Status 1'),"0":__('Status 0')}, formatter: Table.api.formatter.status},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        audit: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/saleer/audit' + location.search,
                    add_url: 'xiservice/saleer/add',
                    edit_url: 'xiservice/saleer/edit',
                    multi_url: 'xiservice/saleer/multi',
                    import_url: 'xiservice/saleer/import',
                    table: 'xiservice_user',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                search: false,     
                showExport: false,
                showToggle: false,
                showColumns: false,                
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), operate: false},
                        {field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'mobile', title: __('Mobile'), visible:false,operate: 'LIKE'},
                        {field: 'mobile_text', title: __('Mobile'), operate: false},
                        {field: 'idcard_front_image', title: __('Idcard_front_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'idcard_back_image', title: __('Idcard_back_image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'checkinfo', title: __('Checkinfo'), searchList: {"0":__('Checkinfo 0'),"1":__('Checkinfo 1'),"2":__('Checkinfo 2')}, formatter: Table.api.formatter.normal},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,                         
                          buttons: [
                                {
                                    name: 'addtabs',
                                    text: __('同意'),
                                    title: __('同意'),
                                    classname: 'btn btn-xs btn-warning  btn-ajax',
                                    icon: 'fa fa-check-square',
                                    url: 'xiservice/saleer/agree?ids={id}',
                                    refresh:true,
                                    visible:function(row){
                                         if(row['checkinfo']==0){
                                             return true;
                                         }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('拒绝'),
                                    title: __('拒绝'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-close',
                                    url: 'xiservice/saleer/refuse?ids={id}',
                                    visible:function(row){
                                         if(row['checkinfo']==0){
                                             return true;
                                         }
                                         return false;
                                    }
                                },
                                {
                                    name: 'addtabs',
                                    text: __('详情'),
                                    title: __('详情'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'xiservice/saleer/edit?ids={id}',
                                    
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                     }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },        
        add: function () {
            Controller.api.bindevent();
        },
        refuse: function () {
            Controller.api.bindevent();
        },        
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
